package com.ns.scs.mod.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.mod.bo.ModBO;
import com.ns.scs.mod.bo.ModPageBO;
import com.ns.scs.mod.dto.ModPageDto;
import com.ns.scs.mod.entry.Mod;
import com.ns.scs.mod.service.IModService;
import com.ns.scs.mod.vo.ModVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 业务 AI 模型参数
 *
 * @author lw
 */
@RestController
@RequestMapping("/mod/")
@RequiredArgsConstructor
@CrossOrigin
public class ModController {

    private final IModService service;

    /**
     * 增
     */
    @PostMapping("add")
    @ReqLog(module = "模型参数模块", name = "增")
    ResultOk add(@RequestBody ModBO bo) {
        Mod mod = new Mod();
        BeanUtils.copyProperties(bo, mod);
        mod.setSort(service.count() + 1);
        return ResultOk.getByBoolean(service.saveOrUpdate(mod));
    }

    /**
     * 删
     *
     * @param id id
     */
    @GetMapping("delete")
    @ReqLog(module = "模型参数模块", name = "删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "模型参数模块", name = "批量删")
    ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 改
     *
     * @param obj 参数
     */
    @PostMapping("update")
    @ReqLog(module = "模型参数模块", name = "改")
    ResultOk update(@RequestBody Mod obj) {
        return ResultOk.getByBoolean(service.updateById(obj));
    }

    /**
     * 查
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "模型参数模块", name = "查")
    ResultOk getById(Long id) {
        Mod data = service.getById(id);
        ModVO vo = new ModVO();
        BeanUtils.copyProperties(data, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查
     *
     * @param code 参数
     */
    @GetMapping("getByCode")
    @ReqLog(module = "模型参数模块", name = "查", authFlag = false)
    ResultOk getByCode(String code) {
        return ResultOk.getSuccessData(service.getByCode(code));
    }

    /**
     * 查 - 集合
     */
    @PostMapping("getList")
    @ReqLog(module = "模型参数模块", name = "查 - 集合")
    ResultOk getList(@RequestBody ModPageBO bo) {
        ModPageDto dto = new ModPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<ModVO> list = service.getList(dto);
        return ResultOk.getPageData(list.getTotal(), list.getRecords());
    }
}
